/**
 * 异步配置路由
 */
import router, { resetRouter } from '../router'
import Layout from '@/layout'
import { getNav } from "@/api/windows";


// 添加路由
function addFn(call) {

    getNav().then(res => {

        // console.log("TCL: res", res)

        let routes = [];

        //////////////////////////////////////////////////////////////////////////////////////
        let childrenWin = [];// windows日志

        res.data.wins.forEach((item, index) => {
            childrenWin.push({
                path: 'log' + index,
                name: 'windowsLog' + index,
                component: () => import('@/views/windows/log1/index'),
                meta: {
                    title: item.host,// IP
                    // icon: 'link',
                    logType: index// 日志服务器选择
                }
            })
        })

        // windows日志
        let route = {
            path: '/windows',
            component: Layout,
            redirect: '/windows/log0',
            name: 'windows',
            meta: {
                title: 'windows日志',
                icon: 'windows'
            },
            children: childrenWin
        };

        routes.push(route);
        router.options.routes.push(route);

        //////////////////////////////////////////////////////////////////////////////////////
        // 数据库
        let childrenDB = [];
        res.data.dbs.forEach((item, index) => {
            
            childrenDB.push({
                path: 'db' + index,
                name: 'Db' + index,
                component: () => import('@/views/database/db/index'),
                meta: {
                    title: item,// IP
                    // icon: 'link'
                },
                children: [{
                    path: 'dbError',
                    component: () => import('@/views/database/db/error'),
                    name: 'DbError',
                    meta: {
                        title: '错误日志',
                        dbType: index// 数据库选择
                    }
                },
                {
                    path: 'dbSuccess',
                    component: () => import('@/views/database/db/success'),
                    name: 'DbSuccess',
                    meta: {
                        title: '成功日志',
                        dbType: index// 数据库选择
                    }
                }]
            })
        })
        
        let routeDB = {
            path: '/database',
            component: Layout,
            redirect: '/database/db1',
            name: 'Database',
            meta: {
                title: '数据库日志',
                icon: 'database'
            },
            children: childrenDB
        };

        routes.push(routeDB);
        router.options.routes.push(routeDB);

        //////////////////////////////////////////////////////////////////////////////////////

        // console.log("TCL: router", routes)
        router.addRoutes(routes);

        // 完成回调
        call && call();

    })
}

export default function addRouters(call) {
    addFn(call);
}